#基于证书密钥认证
from kubernetes import client,config
#config.load_kube_config(r'D:\soft\pycript\devops\config')
#apps_api = client.AppsV1Api()
#for i in apps_api.list_namespaced_deployment(namespace="default").items:
#    print(i.metadata.name)
#基于token
#kubectl create serviceaccount dashboard-admin -n kube-system
# kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
# kubectl describe secrets  -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')
configuration = client.Configuration()
configuration.host = "https://192.168.75.80:6443"
configuration.ssl_ca_cert = r"D:\soft\pycript\devops\ca.crt"
configuration.verify_ssl = True #启用证书
token= " eyJhbGciOiJSUzI1NiIsImtpZCI6ImpGZVNLa3JoSWpQbFBCaHM4NTdlV2FUNUN6Q3EzbUw2VzdCamkzSmgxUW8ifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJkYXNoYm9hcmQtYWRtaW4tdG9rZW4tMnFsYzQiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGFzaGJvYXJkLWFkbWluIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiYmMzODFlYmEtZjdhYy00NDBmLTllMDYtNzUwMjVjM2YwNzkzIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmRhc2hib2FyZC1hZG1pbiJ9.UMQg_1Xsrkty_udel3jD64Nf-0A_Pj2wA3MnMrx_T5PnUAvkQg2eKkhyy4esuAs6ho-3zn_3TQylByrI72_EHTfEXjkIcqw00oPxtuVkE8NhjEuXEMob-oko72tYtQRLvhoHE_-XJBBfmS7cMOnv5XieUeZMSHjqPEo35vK59xzNOceRXf3gk91x-Fac6TzthKfr316z5geJ1cPVnZSVjl1QK54Cm6HfZLLESKsOcIjjiykUYFG_b-YPs9OqNkL4MYFxG6pMzQvtkiYgSgnmHKaGMt-SnstiOg_PLHF5qTzNcydhBUbG7W0BTW9P9zQeICdXemKUAjsp9FTrtvTLzw"
configuration.api_key = {"authorization": "Bearer" + token}
client.Configuration.set_default(configuration)
apps_api = client.AppsV1Api()
for i in apps_api.list_namespaced_deployment(namespace="default").items:
    print(i.metadata.name)
#创建
namespace = "default"
name = "api-test"
replicas = 3
labels = {'a':'1', 'b':'2'}  # 不区分数据类型，都要加引号
image = "nginx"
body = client.V1Deployment(
            api_version="apps/v1",
            kind="Deployment",
            metadata=client.V1ObjectMeta(name=name),
            spec=client.V1DeploymentSpec(
                replicas=replicas,
                selector={'matchLabels': labels},
                template=client.V1PodTemplateSpec(
                    metadata=client.V1ObjectMeta(labels=labels),
                    spec=client.V1PodSpec(
                        containers=[client.V1Container(
                            name="web",
                            image=image
                        )]
                    )
                ),
            )
        )
try:
    apps_api.create_namespaced_deployment(namespace=namespace, body=body)
except Exception as e:
    status = getattr(e, "status")
    if status == 400:
        print(e)
        print("格式错误")
    elif status == 403:
        print("没权限")
# 删除
namespace = "default"
name = "api-test"
apps_api.delete_namespaced_deployment(namespace=namespace, name=name)
